<?php
protectScript('Exception.php');

class ExceptionExt extends Exception{
	protected $_sTableName = 'log';
	protected $_sTablePrefix = TABLE_PREFIX;
	protected $_asmData = '';

	public function __construct(
		$sMessage = null,
		$iCode = null,
		$asmData = null
	){
		parent::__construct($sMessage, $iCode);
		$this->_asmData = $asmData;
	}

	public function addLog($sExceptionName){
		$oMysqliInstance = new AYSMysqli(
			BDD_HOST,
			BDD_USER,
			BDD_PASS,
			BDD_DATABASE
		);

		$aisMessage = array(
				'IP' => $_SERVER['REMOTE_ADDR'],
				'UrlFrom' => @$_SERVER['HTTP_REFERER'],
				'Session' => $_SESSION,
				'Data' => $this->_asmData,
				'POST' => $_POST,
				'GET' => $_GET
		);

		try{
			$oMysqliInstance->query(
				"INSERT INTO ".$this->_sTablePrefix.$this->_sTableName.
				" (date, type, text, level) " .
				"VALUES (
					NOW(),
					'".$sExceptionName."',
					'".$oMysqliInstance->escape_string(serialize($aisMessage))."',
					".ERROR."
				)"
			);
		}catch(MysqliExecutionException $oException){
			$oException->addLog('MysqliExecution');
			$sMessage = $oException->getMessage();
			include_once VIEW_DIR.'/errors.php';
		}
	}
}
